package com.swinboo.im.service.impl;

import com.easemob.im.server.EMService;
import com.huang.entity.result.HCollectionResult;
import com.huang.entity.result.HStringResult;
import com.huang.entity.result.ResponseEnum;
import com.huang.entity.result.ResultBuilder;
import com.swinboo.im.entity.param.AddContactParam;
import com.swinboo.im.entity.param.ContactListParam;
import com.swinboo.im.entity.param.RemoveContactParam;
import com.swinboo.im.service.EMContactService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author Swinboo
 * @date 2022-03-19 20:14
 * @description:
 */

@Service
public class EMContactServiceImpl implements EMContactService {
    @Resource
    private EMService emService;

    @Override
    public HStringResult<Boolean> add(AddContactParam addContactParam) {
        try {
            emService.contact().add(addContactParam.getUid(), addContactParam.getContactUid()).block();
            return ResultBuilder.successHSingle(true);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBuilder.failHSingle("添加好友失败!");
        }
    }

    @Override
    public HStringResult<Boolean> remove(RemoveContactParam removeContactParam) {
        try {
            emService.contact().remove(removeContactParam.getUid(), removeContactParam.getContactUid()).block();
            return ResultBuilder.successHSingle(true);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBuilder.failHSingle("删除好友失败！");
        }
    }

    @Override
    public HCollectionResult<String> list(ContactListParam contactListParam) {
        try {
            return ResultBuilder.successHSCollection(emService.contact().list(contactListParam.getUid()).collectList().block());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBuilder.failHSCollection(ResponseEnum.SERVICE_ERROR);
        }

    }
}
